package com.powergisol.gis.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.powergisol.gis.entity.TabCoordinateSystem;
import com.powergisol.gis.entity.TabZkShuiwei;
import com.powergisol.gis.entity.TabZkTuceng;
import com.powergisol.gis.entity.TabZuankong;
import com.powergisol.gis.service.SaveDataService;
import com.powergisol.gis.util.XyChangeUtil;
import com.powergisol.gis.vo.CnyZkExcelStandardVo;
import com.powergisol.gis.vo.ExcelInFailedDataVo;
import com.powergisol.gis.vo.TaskResultVo;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

import java.math.BigDecimal;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created By marc
 * Date: 2020/9/3  16:07
 * Description:
 */
public class CnyExcelModelListener1 extends AnalysisEventListener {

    private static  final int READ_NUM = 5;

    //组装数据存放
    CnyZkExcelStandardVo vo;

    TabCoordinateSystem system;

    Principal principal;

    SaveDataService saveDataService;

    CacheManager cacheManager;

    Integer settingId;


    String excelName;

    public CnyExcelModelListener1() {
    }

    public CnyExcelModelListener1(CnyZkExcelStandardVo vo, Principal principal, SaveDataService saveDataService, Integer settingId,
                                  CacheManager cacheManager, TabCoordinateSystem system, String excelName) {
        this.vo = vo;
        this.principal = principal;
        this.saveDataService = saveDataService;
        this.settingId = settingId;
        this.cacheManager = cacheManager;
        this.system = system;
        this.excelName =  excelName;
    }

    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
            ArrayList data = (ArrayList) o;
            try{
                //获取钻孔类型
                if(analysisContext.getCurrentRowNum()==4){
                    if(data.size() > READ_NUM) {
                        vo.setZKLX(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //获取钻孔编号
                if(analysisContext.getCurrentRowNum()==5){
                    if(data.size() > READ_NUM) {
                        vo.setZKBH(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //获取经度
                if(analysisContext.getCurrentRowNum()==16){
                    if(data.size() > READ_NUM) {
                        vo.setZKX(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //获取纬度
                if(analysisContext.getCurrentRowNum()==17){
                    if(data.size() > READ_NUM) {
                        vo.setZKY(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //钻孔高程
                if(analysisContext.getCurrentRowNum()==18){
                    if(data.size() > READ_NUM) {
                        vo.setZKBG(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //钻孔直径
                if(analysisContext.getCurrentRowNum()==19){
                    if(data.size() > READ_NUM) {
                        vo.setZKZJ(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //钻孔深度
                if(analysisContext.getCurrentRowNum()==20){
                    if(data.size() > READ_NUM) {
                        vo.setZKSD(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //钻孔地层数
                if(analysisContext.getCurrentRowNum()==37){
                    if(data.size() > READ_NUM){
                        vo.setTCNUM(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
//                //钻孔地下水层数
                vo.setSWNUM("1");
//                if(analysisContext.getCurrentRowNum()==22){
//                    if(data.size() > READ_NUM) {
//                        vo.setSWNUM(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
//                    }
//                    return;
//                }
                //地下水历年最高水位
//                if(analysisContext.getCurrentRowNum()==23){
//                    if(data.size() > READ_NUM){
//                        vo.setZKDXSW(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
//                    }
//                    return;
//                }
//                //地下水近3-5年最高水位
//                if(analysisContext.getCurrentRowNum()==24){
//                    if(data.size() > READ_NUM){
//                        vo.setZKPZTSDXS(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
//                    }
//                    return;
//                }
                //获取地层编号
                if(analysisContext.getCurrentRowNum()==40){
                    if(data.size() > READ_NUM) {
                        List<String> list = new ArrayList<String> ();
                        for (int i = READ_NUM; i < data.size(); i++) {
                            list.add(null == data.get(i)? null : data.get(i).toString());
                        }
                        vo.setTCBH(list);
                    }
                    return;
                }

                //获取层底深度
                if(analysisContext.getCurrentRowNum()==42){
                    if(data.size() > READ_NUM) {
                        List<String> list = new ArrayList<String> ();
                        for (int i = READ_NUM; i < data.size(); i++) {
                            list.add(null == data.get(i)? null : data.get(i).toString());
                        }
                        vo.setTCCDSD(list);
                    }
                    return;
                }

                //获取岩性名称
                if(analysisContext.getCurrentRowNum()==41){
                    if(data.size() > READ_NUM) {
                        List<String> list = new ArrayList<String> ();
                        for (int i = READ_NUM; i < data.size(); i++) {
                            list.add(null == data.get(i)? null : data.get(i).toString());
                        }
                        vo.setTCMC(list);
                    }
                    return;
                }
                //获取直剪粘聚力
//                if(analysisContext.getCurrentRowNum()==40){
//                    if(data.size() > READ_NUM) {
//                        List<String> list = new ArrayList<String> ();
//                        for (int i = READ_NUM; i < data.size(); i++) {
//                            list.add(null == data.get(i)? null : data.get(i).toString());
//                        }
//                        vo.setTCNJL(list);
//                    }
//                    return;
//                }

                //获取直剪内摩擦角
//                if(analysisContext.getCurrentRowNum()==41){
//                    if(data.size() > READ_NUM) {
//                        List<String> list = new ArrayList<String> ();
//                        for (int i = READ_NUM; i < data.size(); i++) {
//                            list.add(null == data.get(i)? null : data.get(i).toString());
//                        }
//                        vo.setTCNMCJ(list);
//                    }
//                    return;
//                }

                //获取稳定水位埋深
                if(analysisContext.getCurrentRowNum()==31){
                    if(data.size() > READ_NUM && null != data.get(READ_NUM)){
                        vo.setSWSD(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
                //获取地下水高程
                if(analysisContext.getCurrentRowNum()==32){
                    if(data.size() > READ_NUM ){
                        vo.setSWDXSW(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }

                //获取施工开始时间
                if(analysisContext.getCurrentRowNum()==100){
                    if(data.size() > READ_NUM) {
                        vo.setZKKSRQ(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }

                //获取施工结束时间
                if(analysisContext.getCurrentRowNum()==101){
                    if(data.size() > READ_NUM) {
                        vo.setZKZZRQ(data.get(READ_NUM) == null ? null : data.get(READ_NUM).toString());
                    }
                    return;
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData(vo);
    }

    private void saveData(CnyZkExcelStandardVo vo){
        try{
            List<ExcelInFailedDataVo> listFailed = new ArrayList<>();//失败数据
            BigDecimal lat = BigDecimal.ZERO;
            BigDecimal lon = BigDecimal.ZERO;
            BigDecimal x = BigDecimal.ZERO;
            BigDecimal y = BigDecimal.ZERO;
            if(1 == vo.getIsChange()){
                lat = XyChangeUtil.change(Double.valueOf(vo.getZKX()), Double.valueOf(vo.getZKY()),system.getCoorChangeLat());
                lon = XyChangeUtil.change(Double.valueOf(vo.getZKX()), Double.valueOf(vo.getZKY()),system.getCoorChangeLon());
            }else{
                lat = new BigDecimal(vo.getZKY());//纬度
                lon = new BigDecimal(vo.getZKX());//经度
            }            //经度-180-180 纬度-90-90
            if (lon.compareTo(new BigDecimal("180")) > 0 || lon.compareTo(new BigDecimal("-180")) < 0 ||
                    lat.compareTo(new BigDecimal("90")) > 0 || lat.compareTo(new BigDecimal("-90")) < 0) {
                ExcelInFailedDataVo failedDataVo = new ExcelInFailedDataVo();
                failedDataVo.setReason("x或y坐标系不在范围内");
                failedDataVo.setRowIndex(excelName);
                listFailed.add(failedDataVo);
                Cache cache = cacheManager.getCache("cache-data");
                TaskResultVo resultVo = new TaskResultVo();
                resultVo.setListFailed(listFailed);
                cache.put(excelName +"sb",resultVo);
                return;
            }
            x = new BigDecimal(vo.getZKX());
            y = new BigDecimal(vo.getZKY());

            Integer tcNum = Integer.valueOf(vo.getTCNUM());
            Integer swNum = Integer.valueOf(vo.getSWNUM());
            List<TabZuankong> listZk = new ArrayList<>();//存放钻孔数据
            List<TabZkTuceng> listTc = new ArrayList<>();//存放土层数据
            List<TabZkShuiwei> listSw = new ArrayList<>();//存放水位数据

            List<String> TCBH = vo.getTCBH();
            List<String> TCCDSD = vo.getTCCDSD();
            List<String> TCMC = vo.getTCMC();
            BigDecimal tccdsd = BigDecimal.ZERO;
            BigDecimal tccdsdPer = BigDecimal.ZERO;
            for(int i = 0;i<tcNum;i++){
                if(1==0){
                    tccdsd = new BigDecimal(TCCDSD.get(i));
                    tccdsdPer = new BigDecimal(TCCDSD.get(i));
                }else{
                    tccdsd = new BigDecimal(TCCDSD.get(i)).subtract(tccdsdPer);
                    tccdsdPer = new BigDecimal(TCCDSD.get(i));
                }
                TabZuankong zk = new TabZuankong();
                TabZkTuceng tc = new TabZkTuceng();
                zk.setZKBH(vo.getZKBH());
                zk.setZKBG(new BigDecimal(vo.getZKBG()));
                zk.setZKLX(vo.getZKLX());
                if(null != vo.getZKSD()){
                    zk.setZKSD(new BigDecimal(vo.getZKSD()));
                }
                if(null != vo.getZKZJ()) {
                    zk.setZKZJ(new BigDecimal(vo.getZKZJ()));
                }
                if(null != vo.getZKDXSW()) {
                    zk.setZKDXSW(new BigDecimal(vo.getZKDXSW()));
                }
                if(null != vo.getZKPZTSDXS()){
                    zk.setZKPZTSDXS(new BigDecimal(vo.getZKPZTSDXS()));
                }
                zk.setZKX(lon);
                zk.setZKY(lat);
                zk.setZKOX(x);
                zk.setZKOY(y);
                zk.setZKKSRQ(vo.getZKKSRQ());
                zk.setZKZZRQ(vo.getZKZZRQ());
                zk.setDataIndex(i+1);
                zk.setSettingId(settingId);
                listZk.add(zk);

                tc.setZKBH(vo.getZKBH());
                tc.setTCCDSD(new BigDecimal(TCCDSD.get(i)));
                tc.setTCHD(tccdsd);
                tc.setTCXH(i+1);
                if(null != TCBH && TCBH.size()>0 && i<TCBH.size()){
                    String tcbh = TCBH.get(i);
                    tc.setTCBH(tcbh);
                    if(null != tcbh && !("").equals(tcbh)){
                        String[] xh = tcbh.split("-");
                        if(xh.length>0 && null != xh[0] && !("").equals(xh[0]) && isNumber(xh[0])){
                            tc.setTCZCBH(Integer.valueOf(xh[0]));
                        }
                        if(xh.length>1 && null != xh[1] && !("").equals(xh[1]) && isNumber(xh[1])){
                            tc.setTCYCBH(Integer.valueOf(xh[1]));
                        }
                        if(xh.length>2 && null != xh[2] && !("").equals(xh[2]) && isNumber(xh[2])){
                            tc.setTCCYCBH(Integer.valueOf(xh[2]));
                        }
                    }
                }

                tc.setDataIndex(i+1);
                tc.setSettingId(settingId);
                if(null != TCMC && TCMC.size()>0 && i<TCMC.size()){
                    tc.setTCMC(TCMC.get(i));
                    //直剪粘聚力
//                    if(i<vo.getTCNJL().size()){
//                        BigDecimal zkzjlx = null == vo.getTCNJL().get(i)?null:new BigDecimal(vo.getTCNJL().get(i));
//                        tc.setTCNJL(zkzjlx);
//                    }
//
//                    //直剪内摩擦角
//                    if(i<vo.getTCNMCJ().size()){
//                        BigDecimal tcnmcj = null == vo.getTCNMCJ().get(i)?null:new BigDecimal(vo.getTCNMCJ().get(i));
//                        tc.setTCNMCJ(tcnmcj);
//                    }
                }
                listTc.add(tc);
            }

//            List<String> swList = vo.getSWSD();
//            if(null != swList){
//                for (int i = 0; i < swList.size();i++){
//                    TabZkShuiwei sw = new TabZkShuiwei();
//                    sw.setDataIndex(i+1);
//                    sw.setZKBH(vo.getZKBH());
//                    sw.setSWSD(new BigDecimal(swList.get(i)));
//                    sw.setSettingId(settingId);
//                    sw.setSWDXSW(new BigDecimal(vo.getSWDXSW()));
//                    listSw.add(sw);
//                }
//            }
            TabZkShuiwei sw = new TabZkShuiwei();
            sw.setDataIndex(1);
            sw.setZKBH(vo.getZKBH());
            sw.setSWSD(null == vo.getSWSD()?null:new BigDecimal(vo.getSWSD()));
            sw.setSettingId(settingId);
            sw.setSWDXSW(null == vo.getSWDXSW()?null:new BigDecimal(vo.getSWDXSW()));
            listSw.add(sw);

            //存库
            if(listZk.size()>0){
                saveDataService.saveBatch(listZk);
            }
            if(listTc.size()>0){
                saveDataService.saveBatch(listTc);
            }
            if(listSw.size()>0){
                saveDataService.saveBatch(listSw);
            }
        }catch (Exception e){
            e.printStackTrace();
            TaskResultVo resultVo = new TaskResultVo();
            List<ExcelInFailedDataVo> listFailed = new ArrayList<>();
            ExcelInFailedDataVo excelInFailedDataVo = new ExcelInFailedDataVo();
            excelInFailedDataVo.setRowIndex(excelName);
            excelInFailedDataVo.setReason("解析失败");
            listFailed.add(excelInFailedDataVo);
            resultVo.setListFailed(listFailed);

            Cache cache = cacheManager.getCache("cache-data");
            cache.put(excelName +"sb",resultVo);
        }

    }

    private boolean isNumber(String value) {
        String matchStr = "^\\d+$";
        return value.matches(matchStr);
    }

}
